#!/usr/bin/bash
# This is the one-liner installation script for these dotfiles. To install,
# run one of these two commands:
#
#     $ wget -qO- "gitee.com/liuyvjin/custom-ohmyzsh/raw/master/my_scripts/updatezsh.sh" | bash
# or
#     $ curl -sL "gitee.com/liuyvjin/custom-ohmyzsh/raw/master/my_scripts/updatezsh.sh" | bash
#
# Author: liujin


ZSH="$HOME/.oh-my-zsh"
ONCE_FLAG=$([[ -d $ZSH ]]; echo $?)  # 初次安装为 1
# SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

# 设置输出颜色
setup_color() {
	# Only use colors if connected to a terminal
	if [ -t 1 ]; then
		RED=$(printf '\033[31m')
		GREEN=$(printf '\033[32m')
		YELLOW=$(printf '\033[33m')
		BLUE=$(printf '\033[34m')
		BOLD=$(printf '\033[1m')
		RESET=$(printf '\033[m')
	else
		RED=""
		GREEN=""
		YELLOW=""
		BLUE=""
		BOLD=""
		RESET=""
	fi
}
setup_color

# 配置 sources.list 为国内镜像地址
setup_sources_once() {
    echo "${GREEN}>>> Setup sources.list.${RESET}"

    # 获得 Ubuntu 版本名, 版本号
    version=$(lsb_release -sc)
    relesenum=$(grep DISTRIB_DESCRIPTION /etc/*-release | awk -F 'Ubuntu ' '{print $2}' | awk -F ' LTS' '{print $1}')
    echo ">>> Your Ubuntu version is: [Ubuntu $version $relesenum]"

    # 获得系统架构
    machine=$(uname --machine)  # 获得系统架构
    grep_option=$([[ $machine =~ arm.* ]] && echo "" || echo "-v")
    echo ">>> Your system architecture is: [$machine]"

    # 将与版本、架构匹配的源写入 /etc/apt/sources.list
    echo ">>> Add sources to /etc/apt/sources.list"
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
    wget -qO- "gitee.com/liuyvjin/custom-ohmyzsh/raw/master/dotfiles/apt/sources.list" | \
    grep $grep_option ubuntu-ports | \
    perl -n -e "s/#version#/${version}/ && print" | \
    sudo tee /etc/apt/sources.list > /dev/null

    # apt update
    sudo apt update
}

# 初次安装一些工具
install_tools_once() {
    echo "${GREEN}>>> Install useful tools.${RESET}"
    sudo apt install -y curl
    sudo apt install -y vim-gtk
    sudo apt install -y nnn
    sudo apt install -y tldr
    sudo apt install -y shellcheck
    sudo apt install -y git zsh
    sudo apt install -y powerline fonts-firacode
    sudo apt install -y tmux
    sudo apt install -y autojump
}

# 初次安装 ohmyzsh
install_ohmyzsh_once() {
    echo "${GREEN}>>> Install oh-my-zsh${RESET}"
    bash -c "$(wget -qO- "gitee.com/liuyvjin/custom-ohmyzsh/raw/master/tools/install.sh")  --unattended"
    sudo chsh -s /bin/zsh "$USER" # 将shell切换为zsh
}

# 更新配置文件
setup_dotfiles(){
    # create links to dotfiles
    echo "${GREEN}>>> Create links to dotfiles${RESET}"
    find "$ZSH"/dotfiles -maxdepth 1 -type f -not -path "*.zshrc" -exec ln -svf "{}" "$HOME" ";"

    echo "${GREEN}>>> Copy files${RESET}"
    # .zshrc 交互式复制
    cp -i -v "$ZSH"/dotfiles/.zshrc "$HOME"/.zshrc  # do not overwrite

    # vim color themes
    if [[ ! -d "$HOME"/.vim/colors ]]; then
        mkdir -p "$HOME"/.vim/colors
    fi
    cp -n -v "$ZSH"/dotfiles/vim_colors/* "$HOME"/.vim/colors  # do not overwrite

    # ssh 客户端配置文件模板
    if [[ ! -d "$HOME"/.ssh ]]; then
        mkdir -p "$HOME"/.ssh
    fi
    cp -n -v "$ZSH"/dotfiles/ssh/* "$HOME"/.ssh/  # do not overwrite

    # 启用 git message template
    git config --global commit.template "$HOME"/.gitmessage
}

# main
if (( ONCE_FLAG > 0 ))     # 执行初次安装的配置
then
    setup_sources_once
    install_tools_once
    install_ohmyzsh_once
else                        # 非初次安装更新仓库
    echo "${GREEN}>>> Updating... ${RESET}"
    cur_path=$(pwd)
    cd $ZSH
    git pull
    cd $cur_path
fi

setup_dotfiles

echo "${YELLOW}>>> Please log out and log back in.${RESET}"